app.factory('CacheFactory', ['$rootScope', '$window', 'uranusPlus', 'localStorageService', function ($rootScope, $window, uranusPlus, localStorageService) {

    setTimeout(function () { 
        option.clearExpiresCache();
    }, 1000);

    //去缓存查询user信息
    var option = {
        min: (1000 * 60),
        hour: (1000 * 60) * 60,
        day: ((1000 * 60) * 60) * 24,
        expiresTime: 86400 * 10,
        isExpires: true, //是否过期
        /*获取的Storage时间,或者判断是否过期*/
        GetExpireStorage: function (key) {
            if (key) {
                key = key.toLowerCase();
            }
            var storagevalue = localStorageService.get(key);
            var nowTime = new Date().getTime();
            if (storagevalue && typeof storagevalue == "object") {
                if (storagevalue && storagevalue.storagetime) {
                    var temptime = new Date(storagevalue.storagetime).getTime();
                    if (nowTime >= temptime) {
                        option.removestorage(key);
                        return null;
                    }
                }
                if (storagevalue.data || storagevalue.data === 0) {
                    return storagevalue.data;
                } else {
                    return storagevalue;
                }
            } else {
                return storagevalue;
            }
        },
        /* 
         * 设置可过期的H5缓存
         * expiresTime 过期时间 毫秒
         * isExpires 是否过期
         */
        SetExpireStorage: function (key, value, config) {
            var tempvalue = {
                data: value
            };
            if (config && config.isExpires) {
                if (config.expiresTime && config.expiresTime > -1) {
                    tempvalue.storagetime = new Date().getTime() + config.expiresTime;
                } else {
                    tempvalue.storagetime = new Date().getTime() + option.expiresTime;
                }
            } else if (option.isExpires) {
                tempvalue.storagetime = new Date().getTime() + option.expiresTime;
            }
            option.setH5Storage(key, tempvalue);
        },
        setUser: function (user) {
            option.setStorage("user", user);
        },
        getUser: function () {
            return option.getStorage("user");
        },
        getUID: function () {
            return option.getStorage("u_uid");
        },
        setUID: function (uid) {
            option.setStorage("u_uid", uid);
        },
        getSourceTypeId: function () {
            return option.getStorage("sourcetypeid");
        },
        setSourceTypeId:function(sourcetypeid){
            option.setStorage("sourcetypeid",sourcetypeid);
        },
        /**
         * 设置H5和app缓存过期时间的缓存
         * userFactory.setStorage("xxx", obj, { isExpires: true, expiresTime: userFactory.min * 30 }); 可以设置过期时间
         */
        setStorage: function (key, value, config) {
            if (key) {
                key = key.toLowerCase();
            }
            if (uranusPlus.isApp) {
                if (config && config.isExpires) {
                    if (!key.startWith("time_")) {
                        key = "time_" + key;
                    }
                    config.expiresTime = config.expiresTime || option.expiresTime;
                }
                uranusPlus.APPStorage.Set(key, value, config);
            } else {
                if (config && config.isExpires) {
                    if (!key.startWith("time_")) {
                        key = "time_" + key;
                    }
                    option.SetExpireStorage(key, value, config);
                } else {
                    var tempvalue = { data: value };
                    option.setH5Storage(key, tempvalue);
                }
            }
        },
        /**
         * 根据设备获取H5或app缓存
         */
        getStorage: function (key) {
            if (key) {
                key = key.toLowerCase();
            }
            if (uranusPlus.isApp) {
                var storage = uranusPlus.APPStorage.Get(key);
                return storage;
            } else {
                return option.GetExpireStorage(key);
            }
        },
        /**
         * 设置H5缓存
         */
        setH5Storage: function (key, value) {
            if (key) {
                key = key.toLowerCase();
            }
            try {
                localStorageService.set(key, value);
            } catch (oException) {
                if (oException.name == 'QuotaExceededError') {
                    // console.log('超出本地存储限额！');
                    //如果内存满了，先删除productindex的缓存
                    option.removestoragebywithstart("time_productindex_");
                    localStorage.setItem(key, value);
                }
            }
        },
        /**
         * 获取H5缓存
         */
        getH5Storage: function (key) {
            if (key) {
                key = key.toLowerCase();
            }
            var storagevalue = localStorageService.get(key);
            if (storagevalue && typeof storagevalue == "object") {
                if (storagevalue.data || storagevalue.data === 0) {
                    return storagevalue.data;
                }
            }
            return storagevalue;
        },
        /**
         * 根据设备删除缓存
         */
        removestorage: function (key) {
            if (uranusPlus.isApp) {
                if (key == 'all') {
                    uranusPlus.APPStorage.DelAll();
                } else if (key == "user") { 
                    uranusPlus.APPStorage.DelUser(); 
                } else {
                    uranusPlus.APPStorage.Del(key);
                }
            } else {
                if (key == 'all') {
                    localStorageService.clearAll();
                } else if (key == "user") {
                    localStorageService.remove("u_uid");
                    localStorageService.remove("user");
                    localStorageService.remove("time_user_transactioninfo"); 
                    localStorageService.remove("time_user_currentinfo"); 
                    localStorageService.remove("GetPlusInterest");
                    option.setStorage("getWeal",'');
                    //删除用户缓存
                    option.removestoragebywithstart("user_");
                } else {
                    localStorageService.remove(key);
                }
            }
        },
        /* 删除以mykey开头H5缓存* */
        removestoragebywithstart: function (mykey) {
            var allkeys = localStorageService.keys();
            for (var key in allkeys) {
                if (typeof allkeys[key] == "string" && allkeys[key].startWith(mykey)) {
                    option.removestorage(allkeys[key]);
                }
            }
        },
        /* 清楚已经过期的缓存 app也要清 */
        clearExpiresCache: function () {
            var allkeys = localStorageService.keys();
            var nowTime = new Date().getTime();
            for (var key in allkeys) {
                var storagevalue = localStorageService.get(key);
                if (storagevalue && typeof storagevalue == "object") {
                    if (storagevalue && storagevalue.storagetime) {
                        var temptime = new Date(storagevalue.storagetime).getTime();
                        if (nowTime >= temptime) {
                            option.removestorage(key);
                        }
                    }
                }
            }
        }
    };
    return option; 
}]);
